import { getHostCommentBase, getHostCommentList } from "../../../api/comment";

import {
    showWxToast,
    showWxLoading,
    hideWxLoading,
} from "../../../utils/index";
Page({
    /**
     * 页面的初始数据
     */
    data: {
        canvasrendered: false,
        src: "",
        rate: 0,
        splid: "",
        showCount: 10,
        currentPage: 1,
        totalPage: 1,
        labelList: [],
        //splidInfo
        avator: "",
        emcee_name: "主持人",
        theme: "婚礼主题",
        hotel_name: "场地",
        using_date: "yyyy-mm-dd",
        banben: "婚礼版",
        xin: "0",
        pcu: "",
        comments: [],
        scene: "",
        status: "",
        loading: false,
        notShareBack: true,
        qrcode: "",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.scene) {
            this.setData({
                splid: options.scene,
                scene: options.scene,
            });
        }
        if (options.splid) {
            this.setData({
                splid: options.splid,
            });
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () { },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (this.data.notShareBack) {
            this.setData({
                currentPage: 1,
                loading: false,
                comments: [],
                totalPage: 1,
            });
            this.getHostCommentBase();
            this.getHostCommentList();
        }
        this.setData({
            notShareBack: true,
        });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () { },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () { },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () { },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.currentPage < this.data.totalPage) {
            this.setData({
                currentPage: this.data.currentPage + 1,
            });
            this.getHostCommentList();
        } else {
            showWxToast("没有更多评价了");
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        this.setData({
            notShareBack: false,
        });
        return {
            title: `${this.data.emcee_name}邀请您查看他的来宾评价`,
            path: `/pages/index/commentShow/commentShow?splid=${this.data.splid}`,
        };
    },
    getHostCommentBase() {
        getHostCommentBase({
            splid: this.data.splid,
        })
            .then((res) => {
                //  
                let { splidInfo, labelList } = res.data;
                if (labelList) {
                    labelList.forEach((item) => {
                        item.name = `${item.bg_name}(${item.size})`;
                    });
                }
                // ;
                this.setData({
                    labelList: res.data.labelList || [],
                    avator: splidInfo.avator,
                    emcee_name: splidInfo.emcee_name,
                    theme: splidInfo.theme,
                    hotel_name: splidInfo.hotel_name,
                    using_date: splidInfo.using_date,
                    xin: res.data.xin.sf_avg,
                    pcu: splidInfo.pcu,
                    scene: splidInfo.scene,
                    status: splidInfo.status,
                    banben: splidInfo.banben,
                });
            })
            .catch((err) => {

            });
    },
    getHostCommentList() {
        if (this.data.loading) {
            // showWxToast('加载中')
            this.setData({
                loading: true,
            });
            return;
        }
        showWxLoading("加载中");
        getHostCommentList({
            splid: this.data.splid,
            showCount: this.data.showCount,
            currentPage: this.data.currentPage,
        })
            .then((res) => {
                hideWxLoading();
                let { list } = res.data;
                list.forEach((item) => {
                    item.time = item.comment_time;
                });

                this.setData({
                    loading: false,
                    comments: this.data.comments.concat(list),
                    totalPage: res.data.totalPage,
                });
            })
            .catch((err) => {

                hideWxLoading();
                this.setData({
                    loading: false,
                });
            });
    },
    change(e) {
        ;
        this.setData({
            rate: e.detail,
        });
    },
});
